package com.jrx.anytxn.param.controller.product;

import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.param.dto.product.PaymentSequenceTableReq;
import com.jrx.anytxn.param.dto.product.PaymentSequenceTableRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.product.IPaymentSequenceTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author by yf
 * @Classname PaymentSequenceTableController
 * @Description 还款顺序表
 * @Date 2019/6/14 0014 下午 13:38
 */
@Api(tags = "页面还款顺序参数API接口")
@RestController
@RequestMapping("/param/paymentSequences")
public class PaymentSequenceTableController {
    private static final Logger logger = LoggerFactory.getLogger(PaymentSequenceTableController.class);

    @Autowired
    private IPaymentSequenceTableService iPaymentSequenceTableService;
    @Autowired
    private InfoService infoService;

    @ApiOperation(value = "根据租户Id分页查询还款顺序表信息", notes = "根据租户Id分页查询还款顺序表信息")
    @GetMapping("/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<PaymentSequenceTableRes>> getAllPaymentSequenceByPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize) {
        TxnRespResult<TxnPage<PaymentSequenceTableRes>> result = new TxnRespResult<>();
        TxnPage<PaymentSequenceTableRes> response;
        try {
            String tenantId = String.valueOf(infoService.getUser().getProjectId());
            response = iPaymentSequenceTableService.findAllPageByTenId(pageNum, pageSize, tenantId);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "新增还款顺序信息", notes = "新增还款顺序信息")
    @PostMapping("/")
    public TxnRespResult<PaymentSequenceTableRes> createPaymentSequence(@RequestBody PaymentSequenceTableReq paymentSequenceTableReq) {
        TxnRespResult<PaymentSequenceTableRes> result = new TxnRespResult<>();
        PaymentSequenceTableRes response;
        try {
            paymentSequenceTableReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            paymentSequenceTableReq.setUpdateBy(infoService.getUser().getUserName());
            response = iPaymentSequenceTableService.addPaymentSequenceTable(paymentSequenceTableReq);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "编辑还款顺序信息", notes = "编辑还款顺序信息")
    @PutMapping("/")
    public TxnRespResult<PaymentSequenceTableRes> modifyPaymentSequence(@RequestBody PaymentSequenceTableReq paymentSequenceTableReq) {
        TxnRespResult<PaymentSequenceTableRes> result = new TxnRespResult<>();
        PaymentSequenceTableRes response;
        paymentSequenceTableReq.setUpdateBy(infoService.getUser().getUserName());
        try {
            response = iPaymentSequenceTableService.modifyPaymentSequenceTable(paymentSequenceTableReq);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID和还款顺序ID查询还款顺序信息", notes = "根据租户ID和还款顺序ID查询还款顺序信息")
    @GetMapping("/{paymentSequenceId}/{tenantId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paymentSequenceId", value = "还款顺序ID", required = true),
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true)
    })
    public TxnRespResult<PaymentSequenceTableRes> getPaymentSequenceById(@PathVariable String paymentSequenceId, @PathVariable String tenantId) {
        TxnRespResult<PaymentSequenceTableRes> result = new TxnRespResult<>();
        PaymentSequenceTableRes res;
        res = iPaymentSequenceTableService.getByTenIdAndPaymentSequenceId(String.valueOf(infoService.getUser().getProjectId()), paymentSequenceId);
        result.getSuccess(res);
        return result;
    }

    @ApiOperation("根据租户ID和还款顺序ID删除还款顺序信息")
    @DeleteMapping("/{paymentSequenceId}/{tenantId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paymentSequenceId", value = "还款顺序ID", required = true),
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true)
    })
    public TxnRespResult<Boolean> removePaymentSequenceById(@PathVariable String paymentSequenceId, @PathVariable String tenantId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);

            flag = iPaymentSequenceTableService.removeByTenIdAndPaymentSequenceId(String.valueOf(infoService.getUser().getProjectId()), paymentSequenceId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }


}
